

// 管理分类

const categoryModel=require('../model/category')
module.exports = {
    getCategoryPage(req, res) {
        // if (req.session.isLogin) {
           res.render('admin/categories');
        // } else {
            // res.send('<script>location.href="http://127.0.0.1:8080/admin/user/login.html"</script>')
        // }
        
    },
    getAllCategories(req,res){
        categoryModel.getAllCategories(data=>{
           let response=data instanceof Array ?{code:200,msg:'ok',data}:{code:500,msg:'获取失败'};
           res.send(response)
        })
    },
    addNewCategoy(req,res){
        let {name,slug,classname}=req.body;
        categoryModel.addNewCategoy(name, slug, classname,result=>{
            // console.log(result);
            if(result.affectedRows===1){
                let {insertId}=result;
                categoryModel.getCategoyById(insertId, data => {
                    // console.log(data);
                    res.send(data?{code:200,msg:'新增并获取成功',data}:{code:200,msg:'新增成功但是获取数据失败，请刷新'})
                })
            }else{
                res.send({code:500,msg:'新增失败'})
            }
        })
    },
    deleteCategoiesById(req,res){
        let {id}=req.query;
        // console.log(req.query);
        categoryModel.deleteCategoiesById(id,result=>{
             res.send(result.affectedRows===1?{code:200,msg:'删除成功'}:{code:500,msg:'删除失败'});
        });
    },
    regainCategoiesById(req,res){
        let {
            id
        } = req.query;
        // console.log(req.query);
        categoryModel.regainCategoiesById(id, result => {
            res.send(result.affectedRows === 1 ? {
                code: 200,
                msg: '恢复成功'
            } : {
                code: 500,
                msg: '恢复失败'
            });
        });
    },
    getCategoryByid(req,res){
        let {id}=req.query;
        // console.log(id);
        categoryModel.getCategoryByid(id,data=>{
            res.send(data?{code:200,msg:'获取成功',data}:{code:500,msg:'获取失败'});
        })
    },
    editCategoryById(req,res){
        let{name,slug,classname,id}=req.body;
        // console.log(req.body);
        categoryModel.editCategoryById(name,slug,classname,id,result=>{
            let response=result.affectedRows===1?{code:200,msg:'修改成功'}:{code:500,msg:'修改失败'};
            res.send(response);
        })
    }
}